package ntd.util.config.common.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.MapSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import io.swagger.annotations.ApiOperation;
import ntd.util.config.common.bo.PageBO;
import ntd.util.frame.data.bo.QueryRuleInfo;
import com.tool.ToolClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

public class ControllerEmptyMethods<T> {

    @Autowired
    private BeanSearcher beanSearcher;

    @Autowired
    private MapSearcher mapSearcher;

    @ApiOperation("自定义查询功能 - 自定义Map类型，详情参考 https://searcher.ejlchina.com/guide/latest/start.html#%E5%BC%80%E5%A7%8B%E6%A3%80%E7%B4%A2")
    @GetMapping("/customer-search-map")
    public SearchResult<Map<String, Object>> customerSearchMap(HttpServletRequest request) {
        return mapSearcher.search(ToolClass.getClassByThis(this), MapUtils.flat(request.getParameterMap()));
    }

    @PostMapping("/customer-search-page")
    @ApiOperation("自定义查询功能 - 分页，详情参考 https://searcher.ejlchina.com/guide/latest/params.html#%E5%88%86%E9%A1%B5%E5%8F%82%E6%95%B0")
    public PageBO<T> customerSearchPage(Map<String, Object> params) {
        SearchResult<T> result =  beanSearcher.search(ToolClass.getClassByThis(this), params);
        return PageBO.transform(result);
    }

    @PostMapping("/customer-search")
    @ApiOperation("自定义查询功能 - 全量查询，详情参考 https://searcher.ejlchina.com/guide/latest/params.html#%E5%88%86%E9%A1%B5%E5%8F%82%E6%95%B0")
    public List<T> customerSearch(Map<String, Object> params) {
        return beanSearcher.searchAll(ToolClass.getClassByThis(this), params);
    }

    @PostMapping("/customer-search-first")
    @ApiOperation("自定义查询功能 - 首个符合条件，详情参考 https://searcher.ejlchina.com/guide/latest/params.html#%E5%88%86%E9%A1%B5%E5%8F%82%E6%95%B0")
    public T customerSearchFirst(Map<String, Object> params) {
        return (T) beanSearcher.searchFirst(ToolClass.getClassByThis(this), params);
    }

    /**
     * 查询条件补充
     * @param queryWrapper
     */
    public void beforeListQuery(QueryWrapper<T> queryWrapper) {
    }

    /**
     * 数据加工
     * @param page
     */
    public void buildView(IPage<T> page) {
        this.buildView(page.getRecords());
    }
    public void buildView(List<T> list) {
        list.forEach(this::buildView);
    }

    /**
     * 对象细节处理
     * @param obj
     */
    public void buildView(T obj) {
    }

    /**
     * 调用详情之后处理
     * @param obj
     */
    public void afterByView(T obj){
    }

    /**
     * 删除之后执行
     * @param obj
     */
    public void afterRemove(T obj) {
    }

    public void beforeEdit(T obj) {
    }

    public void afterEdit(T obj) {
    }

    public void afterAdd(T obj) {
    }

    public T beforeAdd(T obj) {
        return obj;
    }

    /**
     * 动态条件处理
     * @param queryWrapper
     * @param rules
     */
    public void buildRule(QueryWrapper<T> queryWrapper, String rules) {
        for (QueryRuleInfo queryRuleInfo : QueryRuleInfo.transform(rules)) {
            switch (queryRuleInfo.getQueryType()) {
                case "eq":queryWrapper.eq(queryRuleInfo.getName(), queryRuleInfo.getVal());break;
                case "like":queryWrapper.like(queryRuleInfo.getName(), queryRuleInfo.getVal());break;
                case "ge":queryWrapper.ge(queryRuleInfo.getName(), queryRuleInfo.getVal());break;
                case "le":queryWrapper.le(queryRuleInfo.getName(), queryRuleInfo.getVal());break;
                case "in": {
                    queryWrapper.in(queryRuleInfo.getName(), ListUtil.toList(StrUtil.split(queryRuleInfo.getVal(), ","), -99999));break;
                }
            }
        }
    }
}
